const app = getApp()
let _this;
import {urls} from '../../utils/urls'
Page({
  data: {
    avatar : wx.getStorageSync('user').avatar,
    kdtype: [{ la: '小件', price: 2 }, { la: '中件', price: 3 }, { la: '大件', price: 5 }],
    flag:0,
    address_list : '',
    courier_list : '',
    show_address: false,
    show_courier: false,
    address : '',
    courier : '',
    new_address : '',
    new_courier : '',
    desc : ''
  },
  seleCtcourierPoint(){
    _this.setData({
      show_courier : true
    })
  },
  seleAddress(){
    _this.setData({
      show_address : true
    })
  },
  onLoad: function () {
    _this = this
    this.getUserAddrList()
    this.getCourierList()
  },
  getUserAddrList(){
    app.http('GET',urls.addr,{'user':wx.getStorageSync('user').id},
    function(res){
      if(res.code == 100){
        let address = res.results
        let address_list = [] 
        let address_dict = {}
        for(let i in address){
          address_list.push(address[i].addr)
        }
        for (let i in address){
          address_dict[address[i].addr] = address[i].id
        }
        _this.setData({
          address_list : address_list,
          address_dict : address_dict
        })
      }else{
        wx.showToast({
          title: '您还未添加地址！请前往个人页面添加！',
        })
      }
    })
  },
  getCourierList(){
    app.http('GET',urls.addrpoint,{"school":wx.getStorageSync('school').id,"type":0},
    function(res){
      if(res.code == 100){
        let courier = res.results
        let courier_list = [] 
        let courier_dict = {}
        for(let i in courier){
          courier_list.push(courier[i].name)
        }
        for (let i in courier){
          courier_dict[courier[i].name] = courier[i].id
        }
        _this.setData({
          courier_list : courier_list,
          courier_dict : courier_dict
        })
      }else{
        wx.showToast({
          title: '您选择的学校还未添加快递点！',
        })
      }
    })
  },
  showAddress(){
    _this.setData({
      show_address : true
    })
  },
  showCourier(){
    _this.setData({
      show_courier : true
    })
  },
  onAddressCancel(){
    _this.setData({
      new_address : _this.data.address,
      show_address: false
    })
  },
  onAddressConfirm(){
    _this.setData({
      address : _this.data.new_address,
      show_address : false
    })
  },  
  onCourierCancel(){
    _this.setData({
      new_courier: _this.data.courier,
      show_courier: false
    })
  },
  onCourierConfirm(){
    _this.setData({
      courier : _this.data.new_courier,
      show_courier: false
    })
  },
  onAddressChange(event) {
    console.log(event);
    const {value, index } = event.detail;
    _this.setData({
      new_address : value
    })
  },
  onCourierChange(event) {
    console.log(event);
    const {value, index } = event.detail;
    _this.setData({
      new_courier : value
    })
  },
  change(e){
    this.setData({
      flag: e.currentTarget.dataset.index
    })
  },
  descInput(e){
    let desc = e.detail.value
    _this.setData({
      desc : desc
    })
  },
  submit(){
    let task_info ={
      'price': _this.data.kdtype[_this.data.flag].price,
      'desc' : _this.data.desc,
      'size' : _this.data.flag,
      'category_id' : 1,
      'from_addr_id': _this.data.courier_dict[_this.data.courier],
      'to_addr_id' : _this.data.address_dict[_this.data.address],
      'task_status': 1,
      'producer_id' : wx.getStorageSync('user').id,
      'school_id' : wx.getStorageSync('school').id,
    }
    app.http("POST",urls.task,task_info,
    function(res){
      if(res.code == 100){
        wx.showToast({
          title: '您的订单任务已发布！',
        })
        wx.navigateBack()
      }
    })
  }
})